package com.example.neusoft.yuejian_12.Activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.neusoft.yuejian_12.JavaBean.User;
import com.example.neusoft.yuejian_12.Listener.PersonListener;
import com.example.neusoft.yuejian_12.Model.UserModel;
import com.example.neusoft.yuejian_12.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditActivity extends AppCompatActivity {
    private EditText edit_username,edit_password, edit_phone_number,edit_passwordagain;
    private Button btn_back,btn_edit;
    private String user_name,phone_number,user_password,user_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        initViews();
        initData();
        initEvents();
    }

    private void initViews(){
        edit_username = (EditText)findViewById(R.id.edit_username);
        edit_password = (EditText)findViewById(R.id.edit_password);
        edit_passwordagain = (EditText)findViewById(R.id.edit_password_again);
        edit_phone_number = (EditText)findViewById(R.id.edit_telephone);
        btn_back = (Button)findViewById(R.id.edit_btn_back);
        btn_edit = (Button)findViewById(R.id.edit_btn_reg);
        /*获取用户*/
        user_id = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("user_id", String.valueOf(0));
        user_name = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("user_name",String.valueOf(0));
        phone_number = getApplication().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getString("user_tel",String.valueOf(0));
    }

    private void initEvents(){
        btn_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit();
            }
        });

        btn_back.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void edit(){
        UserModel userModel = new UserModel();
        userModel.EditUserPass(null, user_id,edit_password.getText().toString(), personListener);

        UserModel usernameModel = new UserModel();
        usernameModel.EditUserName(null, user_id,edit_username.getText().toString(), personnameListener);
    }

    PersonListener personListener=new PersonListener() {
        @Override
        public void onResponse(Object beanobject) {
            Integer result = (Integer) beanobject;
            String str_psd =edit_password.getText().toString();
            String str_psdagain =edit_passwordagain.getText().toString();
            if(str_psd.equals(str_psdagain)){
                if(result==0){
                    Toast.makeText(EditActivity.this, "修改密码失败", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(EditActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(EditActivity.this,   "两次密码输入不相同", Toast.LENGTH_SHORT).show();
            }
            initData();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(EditActivity.this, "pass success", Toast.LENGTH_SHORT).show();
        }
    };

    PersonListener personnameListener=new PersonListener() {
        @Override
        public void onResponse(Object beanobject) {
            Integer result = (Integer) beanobject;
                if(result==0){
                    Toast.makeText(EditActivity.this, "修改用户名失败", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(EditActivity.this, "修改用户名成功", Toast.LENGTH_SHORT).show();
                }
            initData();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(EditActivity.this, "name success", Toast.LENGTH_SHORT).show();
        }
    };

    private void initData(){
        edit_username.setText(user_name);
        edit_phone_number.setText(phone_number);
    }
}
